package cn.message.service.impl;

import cn.itcast.mapper.merchantMapper;
import cn.itcast.pojo.merchant;
import cn.itcast.pojo.merchantExample;
import cn.message.service.IMerchantService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-05-26 8:58
 */
@Service("merchantService_message")
public class MerchantServiceImpl implements IMerchantService {

    @Resource
    private merchantMapper merchantMapper;

    @Override
    public Map<String, Object> getAllMerchant(Map<String, String> map) {
        merchantExample merchantExample = new merchantExample();
        cn.itcast.pojo.merchantExample.Criteria criteria = merchantExample.createCriteria();

        //添加查询条件
        //根据商家性别查询
        if(!map.get("gender").isEmpty()){
            criteria.andGenderEqualTo(Byte.parseByte(map.get("gender")));
        }
        //根据商家用户名模糊查询
        if(!map.get("merchantNumber").isEmpty()){
            criteria.andMerchantNumberLike("%"+map.get("merchantNumber")+"%");
        }
        //根据商家姓名模糊查询
        if(!map.get("merchantName").isEmpty()){
            criteria.andMerchantNameLike("%"+map.get("merchantName")+"%");
        }

        //返回符合查询条件的总记录数
        int count = merchantMapper.countByExample(merchantExample);

        int page = Integer.parseInt(map.get("page"));
        int limit = Integer.parseInt(map.get("limit"));
        //分页
        PageHelper.startPage(page, limit);
        //查询所有符合条件的商家信息
        List<merchant> merchantList = merchantMapper.selectByExample(merchantExample);
        //格式化日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (merchant merchant : merchantList) {
            if(merchant.getCreateTime() != null){
                merchant.setCreateTimeString(sdf.format(merchant.getCreateTime()));
            }
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",count);
        resultMap.put("merchantList",merchantList);
        return resultMap;
    }

    @Override
    public Boolean addMerchant(merchant merchant) {
        int i = merchantMapper.insertSelective(merchant);
        if(i!=1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean deleteMerchantByMerchantId(int merchantId) {
        int i = merchantMapper.deleteByPrimaryKey(merchantId);
        if(i!=1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean updateMerchant(merchant merchant) {
        int i = merchantMapper.updateByPrimaryKeySelective(merchant);
        if(i!=1){
            return false;
        }
        return true;
    }

    @Override
    public List<merchant> getAllOpenMerchant() {
        merchantExample merchantExample = new merchantExample();
        cn.itcast.pojo.merchantExample.Criteria criteria = merchantExample.createCriteria();
        criteria.andMerchantIsopenEqualTo("1");

        List<merchant> merchantList = merchantMapper.selectByExample(merchantExample);
        return merchantList;
    }
}
